package com.example.blogsystem.controller;

import com.example.blogsystem.exception.BlogException;
import com.example.blogsystem.model.common.Result;
import com.example.blogsystem.model.dto.BlogDto;
import com.example.blogsystem.model.vo.BlogVo;
import com.example.blogsystem.service.BlogService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 约定controller类只校验前端参数的合法性
 */

@RequestMapping("/blog")
@RestController
@Tag(name = "文章请求")
public class BlogController {

    @Autowired
    private BlogService blogService;

    //全量查询博客列表
    @GetMapping("/getBlogList")
    @Operation(summary = "全量查询博客列表")
    public Result getBlogList(){
        List<BlogVo> blogVos = blogService.queryAllBlogs();
        return Result.success(blogVos);
    }

    //根据博客ID查询
    @GetMapping("/getBlogById")
    @Operation(summary = "根据博客ID查询")
    public Result getBlogById(BlogDto blogDto, HttpServletRequest request){
        if(blogDto == null){
            throw new BlogException("参数异常！");
        }
        Integer blogId = blogDto.getId();
        //校验参数的合法性
        if(blogId == null){
            throw new BlogException("参数为空");
        }
        if(blogId <= 0){
            throw new BlogException("博客请求不存在！");
        }
        //查询文章
        BlogVo blog = blogService.queryBlogById(blogId);
        //判断当前文章是不是登录者
//        Integer loginUserId = userService.getLoginUser(request);
//        if(loginUserId != null && loginUserId.equals(blog.getUserId())){
//            blog.setLoginUser(1);
//        }
        return Result.success(blog);
    }

    //新增博客
    @PostMapping("/addBlog")
    @Operation(summary = "新增博客")
    public Result addBlog(BlogDto blogDto, HttpServletRequest request){
        //校验参数
        if(blogDto == null){
            throw new BlogException("用户输入有误");
        }
        if(!StringUtils.hasLength(blogDto.getTitle())){
            throw new BlogException("文章标题为空");
        }
        if(!StringUtils.hasLength(blogDto.getContent())){
            throw new BlogException("文章内容为空");
        }
        if(!StringUtils.hasLength(blogDto.getSummary())){
            throw new BlogException("摘要内容为空");
        }
        if(blogDto.getSummary().length() > 200){
            throw new BlogException("摘要字数超过200");
        }

        //获取当前上传者的信息
//        Integer loginUserId = userService.getLoginUser(request);
//        if(loginUserId == null){
//            return Result.unLogin();
//        }
//        blog.setUserId(loginUserId);
//        //上传博客
//        blogService.insertBlog(blog);

        return Result.success("");
    }

    //更新博客
    @PostMapping("/updateBlog")
    @Operation(summary = "更新博客")
    public Result updateBlog(BlogDto newBlog, HttpServletRequest request){
        //校验参数
        if(newBlog.getId() == null || newBlog.getId() <= 0){
            return Result.fail("非法输入");
        }
        //判断是否非法修改
//        Integer loginUserId = userService.getLoginUser(request);
//        Integer userId = blogService.getUserIdById(newBlog.getId());
//        if(loginUserId == null || !loginUserId.equals(userId)){
//            return Result.fail("登录用户不一致，非法修改！");
//        }
        //进行修改
//        blogService.updateBlog(newBlog);

        return Result.success("");
    }

    //删除博客
    @PostMapping("/deleteBlog")
    @Operation(summary = "删除博客")
    public Result deleteBlog(Integer blogId, HttpServletRequest request) {
        //校验参数
        if (blogId == null || blogId <= 0) {
            return Result.fail("非法输入");
        }
        //判断是否非法删除
//        Integer loginUserId = userService.getLoginUser(request);
//        Blog blog = blogService.queryBlogById(blogId);
//        if (loginUserId == null || !loginUserId.equals(blog.getUserId())) {
//            return Result.fail("删除失败");
//        }

        //删除博客
//        blogService.deleteBlog(blog.getId());
        return Result.success("");
    }

}
